package com.endoc.endocbtool.viewmodel;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.view.View;
import androidx.lifecycle.MutableLiveData;
import com.endoc.base.utils.LogUtils;
import com.endoc.btmodule.BaseBleViewModel;
import com.endoc.btmodule.BleModel;
import com.endoc.btmodule.DataUtils;
import com.endoc.endocbtool.bean.JCLBean;

import java.util.ArrayList;
import java.util.List;

import cn.com.heaton.blelibrary.ble.model.BleDevice;

public class MainViewModel extends BaseBleViewModel<BleModel>{
       String TAG ="MainViewModel";
       public MutableLiveData<Integer> deviceListVisibleLiveData = new MutableLiveData<>();
       //大剂量输入
       public MutableLiveData<String>  djlMutableLiveData = new MutableLiveData<>();
       //日总量输入
       public MutableLiveData<String>  rzlMutableLiveData = new MutableLiveData<>();
       //胰岛素厂家输入
       public MutableLiveData<String>  ydscjMutableLiveData = new MutableLiveData<>();
       //报警类型输入
       public MutableLiveData<String>  bjlxMutableLiveData = new MutableLiveData<>();
       //方波数据
       public MutableLiveData<String>  fbMutableLiveData = new MutableLiveData<>();
       //方波时间
       public MutableLiveData<String>  fbTimeMutableLiveData = new MutableLiveData<>();
       //双波数据
       public MutableLiveData<String>  sbMutableLiveData = new MutableLiveData<>();
       //双波时间
       public MutableLiveData<String>  sbTimeMutableLiveData = new MutableLiveData<>();
       //排气
       public MutableLiveData<String>  paiqiMutableLiveData = new MutableLiveData<>();

       //闭环剂量
       public MutableLiveData<String>  bhjlMutableLiveData = new MutableLiveData<>();

       //基础率设置
       public MutableLiveData<List<JCLBean>> jclBeanMutableLiveData = new MutableLiveData<>();

       //单条基础率设置
       public MutableLiveData<String> simpleJclBeanMutableLiveData = new MutableLiveData<>();

       //临时基础率设置
       public MutableLiveData<String> lsJclMutableLiveData = new MutableLiveData<>();
       //临时基础率增速设置
       public MutableLiveData<String> lsJclZSMutableLiveData = new MutableLiveData<>();

       //更新时间
       public MutableLiveData<String>  updateTimeYearMutableLiveData = new MutableLiveData<>();
       public MutableLiveData<String>  updateTimeMouthMutableLiveData = new MutableLiveData<>();
       public MutableLiveData<String>  updateTimeDayMutableLiveData = new MutableLiveData<>();
       public MutableLiveData<String>  updateTimeHourMutableLiveData = new MutableLiveData<>();
       public MutableLiveData<String>  updateTimeMinMutableLiveData = new MutableLiveData<>();
       public MutableLiveData<String>  updateTimeMiaoMutableLiveData = new MutableLiveData<>();
       //发送数据
       public MutableLiveData<String>  sendDataMutableLiveData = new MutableLiveData<>();
       //接收数据
       public MutableLiveData<String>  receiveDataMutableLiveData = new MutableLiveData<>();
       //大剂量输注
       public MutableLiveData<String>  djlszMutableLiveData = new MutableLiveData<>();
       //日总量时间选择
       public MutableLiveData<Long>  rzlTimerMutableLiveData = new MutableLiveData<>();
       //日总量编号
       public MutableLiveData<String>  rzlNumberMutableLiveData = new MutableLiveData<>();

       //排气时间选择
       public MutableLiveData<Long>  pqTimerMutableLiveData = new MutableLiveData<>();
       //排气编号
       public MutableLiveData<String>  pqNumberMutableLiveData = new MutableLiveData<>();

       //24段时间
       public MutableLiveData<Long>  jcl24TimerMutableLiveData = new MutableLiveData<>();
       //24段编号
       public MutableLiveData<String>  jcl24NumberMutableLiveData = new MutableLiveData<>();

       //48段时间
       public MutableLiveData<Long>  jcl48TimerMutableLiveData = new MutableLiveData<>();
       //48段编号
       public MutableLiveData<String>  jcl48NumberMutableLiveData = new MutableLiveData<>();

       //报警时间
       public MutableLiveData<Long>  alarmTimerMutableLiveData = new MutableLiveData<>();
       //报警编号
       public MutableLiveData<String>  alarmNumberMutableLiveData = new MutableLiveData<>();

       //胰岛素厂家时间
       public MutableLiveData<Long>  insulinTimerMutableLiveData = new MutableLiveData<>();
       //胰岛素厂家编号
       public MutableLiveData<String>  insulinNumberMutableLiveData = new MutableLiveData<>();

       //螺杆复位时间
       public MutableLiveData<Long>  lgfwTimerMutableLiveData = new MutableLiveData<>();
       //螺杆复位编号
       public MutableLiveData<String>  lgfwNumberMutableLiveData = new MutableLiveData<>();


       //单波时间
       public MutableLiveData<Long>  danBoTimerMutableLiveData = new MutableLiveData<>();
       //单波编号
       public MutableLiveData<String>  danBoNumberMutableLiveData = new MutableLiveData<>();

       //方波时间
       public MutableLiveData<Long>  fangBoTimerMutableLiveData = new MutableLiveData<>();
       //方波编号
       public MutableLiveData<String>  fangBoNumberMutableLiveData = new MutableLiveData<>();
       //双波时间
       public MutableLiveData<Long>  shuangBoTimerMutableLiveData = new MutableLiveData<>();
       //双波编号
       public MutableLiveData<String>  shuangBoNumberMutableLiveData = new MutableLiveData<>();
      //重命名
      public MutableLiveData<String>  reNameMutableLiveData = new MutableLiveData<>();
    //打药补发
    public MutableLiveData<String>  dybfLiveData = new MutableLiveData<>();
      //收到的原始数据
      public MutableLiveData<List<String>> originMutableLiveData = new MutableLiveData<List<String>>();
     List<String>  mList = new ArrayList<>();

       @Override
       public void onLeScan(BleDevice bleDevice, int rssi, byte[] scanRecord) {
              //如果设备包含endoc
              //if(bleDevice.getBleName()!=null&&(bleDevice.getBleName().contains("Pump1")||bleDevice.getBleName().contains("Endoc"))){
              if (bleDevice.getBleName() != null && (bleDevice.getBleName().contains("Pump") || bleDevice.getBleName().contains("Endoc"))) {
                     //因为Android相同device会进行复用,所以直接判断device对象即可
                     if (!scanDeviceListMutableLiveData.getValue().contains(bleDevice)) {
                            LogUtils.LogD(TAG, "bleDevice:" + bleDevice.getBleName());
                            List<BleDevice> value = scanDeviceListMutableLiveData.getValue();
                            value.add(bleDevice);
                            scanDeviceListMutableLiveData.setValue(value);
                     }
              }
       }




       @Override
       public void onConnectionChanged(BleDevice device) {
              if (device.isConnected()) {
                     deviceListVisibleLiveData.setValue(View.GONE);
              } else {
                     deviceListVisibleLiveData.setValue(View.VISIBLE);
              }
              connectDeviceLiveData.setValue(device);
       }

       @Override
       public void onReady(BleDevice device) {

              //BleTools.enableUUIDNotify(device, UUID.fromString("0000fE40-0000-1000-8000-00805f9b34fb"),UUID.fromString("0000FE42-0000-1000-8000-00805f9b34fb"));
              //设置mtu
              bleModel.setMTU(connectDeviceLiveData.getValue(),116);
       }

       @Override
       public void receiveData(BleDevice device, BluetoothGattCharacteristic characteristic) {
              byte[] bytes = characteristic.getValue();
           if(bytes[1]>=0x40&&bytes[1]<=0x4d||bytes[1]==0x70||bytes[1]==0x71||bytes[1]==0x72){//显示列表
                   mList.add(DataUtils.bytesToHexString(bytes, bytes.length));
                   originMutableLiveData.postValue(mList);
           }
              String receiveData = DataUtils.bytesToHexString(bytes, bytes.length);
              //收到硬件数据
              LogUtils.LogD(TAG, "收到的数据:" + receiveData);
             receiveDataMutableLiveData.postValue("收到的数据:" + receiveData);
       }

       @Override
       public void onWriteSuccess(BleDevice device, BluetoothGattCharacteristic characteristic) {
              String sendData = DataUtils.bytesToHexString(characteristic.getValue(), characteristic.getValue().length);
              LogUtils.LogD(TAG, "发送的数据:" + sendData);
             sendDataMutableLiveData.postValue("发送的数据:" + sendData);
       }

       @Override
       public void onWriteFailed(BleDevice device, int failedCode) {

       }

       @Override
       public void onMtuChanged(BleDevice device, int mtu, int status) {
              LogUtils.LogD(TAG,"device name:"+device.getBleName()+"  mtu:"+mtu+"  "+status);
              if (BluetoothGatt.GATT_SUCCESS == status) {
                     bleModel.enableNotify(device);
                     LogUtils.LogD(TAG,"onMtuChanged success MTU = " + mtu);
              } else {
                     LogUtils.LogD(TAG,"onMtuChanged fail ");
              }
       }


       @Override
       public BleModel createModel() {
              return new BleModel();
       }
}
